def jwt_response_payload_handler(token,user=None,request=None):

    """
    自定义jwt 认证成功返回数据
    :param token:
    :param user:
    :param request:
    :return:
    """

    return {
        'token':token,
        'user_id':user.id,
        'username':user.username
    }

import re
from users.models import User
from django.contrib.auth.backends import ModelBackend


def get_user_by_account(username):

    try:
        if re.match(r'1[3-9]\d{9}',username):
            user = User.objects.get(mobile=username)

        else:

            user = User.objects.get(username=username)

    except User.DoesNotExist:

        user = None

    return user

class UsernameMobileAuthBackend(ModelBackend):

    def authenticate(self, request, username=None, password=None, **kwargs):

        """
        思路：
        用户输入用户名（手机号）之后，
        我们根据 用户输入的用户名（手机号）和密码 进行校验
        如果成功 返回user

        :param request:
        :param username:
        :param password:
        :param kwargs:
        :return:
        """

        user = get_user_by_account(username=username)

        if user is not None and user.check_password(password):


            return user

        return None

    def get_user(self, user_id):

        try:
            return User.objects.get(pk=user_id)

        except User.DoesNotExist:
            return None

